package com.zhu.androidsample;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ViewFlipper;

import com.zhu.androidsample.business.MainActivity;

/**
 * 闪屏页面/引导页面
 * 程序第一次运行时候，或者版本更新的时候显示。
 */
public class StartActivity extends AppCompatActivity implements View.OnTouchListener, GestureDetector.OnGestureListener, View.OnClickListener {
    public static final int LOADING_MAX_COUNT = 3;
    ViewFlipper mViewFlipper;
    Button start;
    GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        mViewFlipper = (ViewFlipper) this.findViewById(R.id.start_viewflipper);
        start = (Button) findViewById(R.id.start_btn_start);

        init();
    }

    private void init() {

        mViewFlipper.setOnTouchListener(this);//将点击事件传递给mGestureDetector
        start.setOnClickListener(this);
        mGestureDetector = new GestureDetector(this, this);//解析好手势后，执行回调函数。
    }

    /**
     * View.OnTouchListener
     * 接口手势回调方法
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        return true;
    }

    /**
     * GestureDetector.OnGestureListener
     * 接口手势回调方法
     */
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        if (e2.getX() - e1.getX() > 0) {
            //向右划
            if (mViewFlipper.getDisplayedChild() > 0) {
                mViewFlipper.setInAnimation(getApplicationContext(), R.anim.slide_previous_in);
                mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.slide_previous_out);
                mViewFlipper.showPrevious();
            }
        } else {
            //向左划
            if (mViewFlipper.getDisplayedChild() < LOADING_MAX_COUNT) {
                mViewFlipper.setInAnimation(getApplicationContext(), R.anim.slide_next_in);
                mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.slide_next_out);
                mViewFlipper.showNext();
            }
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start_btn_start:
                Intent intent = getIntent();
                intent.setClass(this, MainActivity.class);
                startActivity(intent);
                StartActivity.this.finish();
        }
    }
}
